#include "AppManager.h"

static const uint8_t weather_warning_bits[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x3f, 0x02, 0x00, 0x50, 0x55,
    0x02, 0x0c, 0xb0, 0x6a, 0x02, 0x12, 0xd0, 0x5f, 0x02, 0x12, 0xb0, 0x68,
    0x02, 0x21, 0xd0, 0x5a, 0x82, 0x4c, 0xb0, 0x68, 0x82, 0x4c, 0xd0, 0x5f,
    0x42, 0x8c, 0xb0, 0x6a, 0x42, 0x8c, 0x50, 0x55, 0x22, 0x00, 0xb1, 0x6f,
    0x12, 0x0c, 0xd2, 0x58, 0x12, 0x0c, 0xb2, 0x6a, 0x0a, 0x00, 0xd4, 0x58,
    0xfa, 0xff, 0xb7, 0x6f, 0x02, 0x00, 0x50, 0x55, 0xfe, 0xff, 0xff, 0x7f,
    0xfe, 0x07, 0x00, 0x40, 0x5e, 0x07, 0x00, 0x40, 0xae, 0xb7, 0x6d, 0x5b,
    0x5e, 0xb7, 0x6d, 0x5b, 0xae, 0x07, 0x00, 0x40, 0xfe, 0x07, 0x00, 0x40,
    0xfc, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

class AppWarning : public AppBase
{
private:
    /* data */
public:
    AppWarning()
    {
        name = "warning";
        title = "天气预警";
        description = "天气预警信息";
        image = weather_warning_bits;
        _showInList = true;
        _reentrant = false;
    }
    void setup();
};
static AppWarning app;

void AppWarning::setup()
{
    if(digitalRead(PIN_BUTTONL) == 0 || digitalRead(PIN_BUTTONR) == 0 || digitalRead(PIN_BUTTONC) == 0)
    {
        appManager.goBack();
        return;
    }
    display.clearScreen();
    if (weather.hasAlert)
    {
        GUI::drawWindowsWithTitle(weather.alertTitle, 0, 0, 296, 128);
        u8g2Fonts.setBackgroundColor(1);
        u8g2Fonts.setForegroundColor(0);
        u8g2Fonts.setCursor(18 + 24, 31);
        char *str = weather.alert;
        while (*str)
        {
            if (u8g2Fonts.getCursorX() >= 296 - 16)
            {
                u8g2Fonts.setCursor(18, u8g2Fonts.getCursorY() + 15);
            }
            u8g2Fonts.print(*str);
            str++;
        }
        display.display(false);
    }
    else
    {
        u8g2Fonts.drawUTF8(75, 100, "无预警，3秒后自动退出");
        display.display(false);
        delay(3000);
        appManager.goBack();
        return;
    }
    appManager.noDeepSleep = false;
}
